#include "GL/glew.h"
#include "LoadShader.h"
#include "utility.h"
#include "vmath.h"
#include "loadDDSTexture.h"

GLuint program, program2, vao, vbo;
GLuint tex2D = -1;

GLfloat vert[] =
{
	0.9f, 0.9f,  -0.9f, -0.9f,   0.9f, -0.9f,
	0.9f, 0.9f,  -0.9f, -0.9f,   -0.9f, 0.9f,

	1.0f, 0.0f,  0.0f, 1.0f,   1.0f, 1.0f,
	1.0f, 0.0f,  0.0f, 1.0f,   0.0f, 0.0f
};

//GLfloat vert[] =
//{
//	0.9f, 0.9f,  -0.9f, -0.9f,   0.9f, -0.9f,
//	0.9f, 0.9f,  -0.9f, -0.9f,   -0.9f, 0.9f,
//
//	0.9f, 0.3f,  0.4f, 0.9f,   0.9f, 0.9f,
//	0.9f, 0.3f,  0.4f, 0.9f,   0.4f, 0.3f
//};

bool init()
{
	ShaderInfo si[] = { { GL_VERTEX_SHADER, "Shader.vert" },{ GL_FRAGMENT_SHADER, "Shader.frag" },{ GL_NONE, nullptr } };
	program = LoadShade(si);

	ShaderInfo si2[] = { { GL_VERTEX_SHADER, "Shader2.vert" },{ GL_FRAGMENT_SHADER, "Shader2.frag" },{ GL_NONE, nullptr } };
	program2 = LoadShade(si2);

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vert), vert, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(float) * 6 * 2));
	glEnableVertexAttribArray(1);


	vglImageData image;
	vglLoadImage("pic.dds", &image);
	tex2D = vglLoadTexture(&image);
	glBindTexture(GL_TEXTURE_2D, tex2D);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


	glClearColor(0.0f, 0.05f, 0.05f, 1.0f);
	return true;
}

void Display(double time)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glUseProgram(program2);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, tex2D);
	glUniform1i(100, 0);

	Matrix4f m = Matrix4f::createTranslation(-0.2, 0.2, 0.0) * Matrix4f::createScale(0.5f, 0.5f, 0.5f);

	//glUniformMatrix4fv(1, 1, GL_FALSE, (float*)m);
	//glBindVertexArray(vao);
	//glDrawArrays(GL_TRIANGLES, 0, 6);
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

	glUseProgram(program);
	glUniform4f(100, 0.0f, 0.00f, 0.60f, 0.6f);
	m = Matrix4f::createTranslation(0.2, -0.2, 0.0) * Matrix4f::createScale(0.5f, 0.5f, 0.5f);
	glUniformMatrix4fv(1, 1, GL_FALSE, (float*)m);
	glBindVertexArray(vao);
	glDrawArrays(GL_TRIANGLES, 0, 6);

	glUseProgram(program);
	glUniform4f(100, 0.6f, 0.00f, 0.00f, 0.6f);
	m = Matrix4f::createTranslation(-0.2, 0.2, 0.0) * Matrix4f::createScale(0.5f, 0.5f, 0.5f);
	glUniformMatrix4fv(1, 1, GL_FALSE, (float*)m);
	glBindVertexArray(vao);
	glDrawArrays(GL_TRIANGLES, 0, 6);





}

void onClose()
{
	glDeleteVertexArrays(1, &vao);
	glDeleteBuffers(1, &vbo);
}

